﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NoteBox
{
    [Serializable]
    class mTask : Panel
    {
        #region Обьявление кнопок и прочих компонентов
        private Button buttonTaskClose;
        public RichTextBox richTextBox1;
        private Button buttonTaskDown;
        private Button buttonTaskUP;
        private Button buttonTime;
        public Label labelTime;
        public TextBox textBox1;
        public Button buttonTaskHide;
        private ToolTip toolTipa;
        private Button buttonStrikeout;
        //
        private Panel panelDel;
        private Button buttonDelYes;
        private Button buttonDelNo;
        private Label labelDelAsk;
        //
        private System.Windows.Forms.Button zbuttonpanel;
        private System.Windows.Forms.Button zbuttonrichtext;
        #endregion
        //
        #region Время, дата напоминание. Ее создание
        /// <summary>
        /// Создание времени, которое будет показываться в labeltime
        /// </summary>
        public void RemindTimeCreate()
        {
            string min;
            if (RemindTimeMinute < 10)
                min = "0" + RemindTimeMinute.ToString();
            else
                min = RemindTimeMinute.ToString();
            labelTime.Text = RemindTimeDate.ToShortDateString() + ", " + RemindTimeHour.ToString() + ":" + min;
        }
        public DateTime date = DateTime.Parse("01.01.1001");
        public DateTime RemindTimeDate
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }

        public int hour = -1;
        public int RemindTimeHour
        {
            get
            {
                return hour;
            }
            set
            {
                hour = value;
            }
        }

        public int minute = -1;
        public int RemindTimeMinute
        {
            get
            {
                return minute;
            }
            set
            {
                minute = value;
            }
        }

        #endregion


        private int panelOldHeight = 0;
        public int PanelOldHeight
        {
            get
            {
                return panelOldHeight;
            }
            set
            {
                panelOldHeight = value;
            }
        }
        //
        private int id = 0;
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        //
        //
        bool active=false; // активно ли жанное напоминание
        public bool Active
        {
            get
            {
                return active;
            }
        }
        //
        public event dInt HidePressed;
        public event dInt ClosePressed;
        public event dInt TaskUpPressed;
        public event dInt TaskDownPressed;
        public event dInt TimePressed;
        public event dInt HeightResized;
        //
        public mTask(int _id, string _name,int _topPosition)
        {
            id = _id;
            panelOldHeight = Height;
            

            buttonTaskHide = new Button();
            textBox1 = new TextBox();
            labelTime = new Label();
            buttonTime = new Button();
            richTextBox1 = new RichTextBox();
            buttonTaskDown = new Button();
            buttonTaskUP = new Button();
            buttonTaskClose = new Button();
            toolTipa = new ToolTip();
            buttonStrikeout = new Button();
            //
            panelDel = new Panel();
            labelDelAsk = new Label();
            buttonDelNo = new Button();
            buttonDelYes = new Button();
            //
            zbuttonpanel = new System.Windows.Forms.Button();
            zbuttonrichtext = new System.Windows.Forms.Button();
            // 
            // panel1
            // 
            Controls.Add(panelDel);
            Controls.Add(buttonTaskHide);
            Controls.Add(textBox1);
            Controls.Add(labelTime);
            Controls.Add(buttonTime);
            Controls.Add(richTextBox1);
            Controls.Add(buttonTaskDown);
            Controls.Add(buttonTaskUP);
            Controls.Add(buttonTaskClose);
            Controls.Add(buttonStrikeout);
            Controls.Add(this.zbuttonrichtext);
            Controls.Add(this.zbuttonpanel);
            MouseMove+=new MouseEventHandler(Resize_MouseMove);
            MouseUp+=new MouseEventHandler(Resize_MouseUp);
            MouseDown+=new MouseEventHandler(Resize_MouseDown);
            Location = new System.Drawing.Point(6,_topPosition);
            Name = _name;
            Size = new System.Drawing.Size(257, 100);
            // 
            // buttonTaskHide
            // 
            buttonTaskHide.BackgroundImage = global::NoteBox.Properties.Resources.ButtonHide;
            buttonTaskHide.FlatStyle = FlatStyle.Popup;
            buttonTaskHide.Font = new System.Drawing.Font("Microsoft Sans Serif", 5.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            buttonTaskHide.Location = new System.Drawing.Point(6, 4);
            buttonTaskHide.Name = "buttonTaskHide";
            buttonTaskHide.Size = new System.Drawing.Size(16, 15);
            buttonTaskHide.TabIndex = 18;
            buttonTaskHide.UseVisualStyleBackColor = true;
            buttonTaskHide.Click+=new EventHandler(mbuttonTaskHide_Click);
            toolTipa.SetToolTip(buttonTaskHide, "Свернуть/развернуть");
            // 
            // panelDel
            // 
            panelDel.Controls.Add(labelDelAsk);
            panelDel.Controls.Add(buttonDelNo);
            panelDel.Controls.Add(buttonDelYes);
            panelDel.Location = new System.Drawing.Point(32, 32);
            panelDel.Name = "panelDel";
            panelDel.Size = new System.Drawing.Size(150, 30);
            panelDel.TabIndex = 21;
            panelDel.Visible = false;
            // 
            // labelDelAsk
            // 
            labelDelAsk.Image = global::NoteBox.Properties.Resources.DelAsk;
            labelDelAsk.Location = new System.Drawing.Point(0, 0);
            labelDelAsk.Name = "labelDelAsk";
            labelDelAsk.Size = new System.Drawing.Size(50, 30);
            labelDelAsk.TabIndex = 2;
            // 
            // buttonDelNo
            // 
            buttonDelNo.BackgroundImage = global::NoteBox.Properties.Resources.DelAskNO;
            buttonDelNo.FlatStyle = FlatStyle.Popup;
            buttonDelNo.Location = new System.Drawing.Point(100, 0);
            buttonDelNo.Name = "buttonDelNo";
            buttonDelNo.Size = new System.Drawing.Size(50, 30);
            buttonDelNo.TabIndex = 1;
            buttonDelNo.UseVisualStyleBackColor = true;
            buttonDelNo.Click+=new EventHandler(mbuttonDelNo_Click);
            // 
            // buttonDelYes
            // 
            buttonDelYes.BackgroundImage = global::NoteBox.Properties.Resources.DelAskYes;
            buttonDelYes.FlatStyle = FlatStyle.Popup;
            buttonDelYes.Location = new System.Drawing.Point(50, 0);
            buttonDelYes.Name = "buttonDelYes";
            buttonDelYes.Size = new System.Drawing.Size(50, 30);
            buttonDelYes.TabIndex = 0;
            buttonDelYes.UseVisualStyleBackColor = true;
            buttonDelYes.Click+=new EventHandler(mbuttonDelYes_Click);            
            // 
            // textBox1
            // 
            textBox1.BackColor = System.Drawing.Color.Black;
            textBox1.BorderStyle = BorderStyle.None;
            textBox1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            textBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(153)))), ((int)(((byte)(180)))), ((int)(((byte)(210)))));
            textBox1.Location = new System.Drawing.Point(27, 5);
            textBox1.Name = "textBox1";
            textBox1.Size = new System.Drawing.Size(115, 13);
            textBox1.TabIndex = 0;
            textBox1.TextAlign = HorizontalAlignment.Center;
            textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
            // 
            // labelTime
            // 
            labelTime.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
            labelTime.AutoSize = true;
            labelTime.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            labelTime.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(153)))), ((int)(((byte)(180)))), ((int)(((byte)(210)))));
            labelTime.Location = new System.Drawing.Point(143, 4);
            labelTime.Name = "labelTime";
            labelTime.Size = new System.Drawing.Size(85, 14);
            labelTime.TabIndex = 16;
            labelTime.Text = "";
            labelTime.UseMnemonic = false;
            labelTime.Visible = true;
            //toolTipa.SetToolTip(labelTime, "Время напоминания");
            // 
            // buttonTime
            // 
            buttonTime.Anchor = ((AnchorStyles)((AnchorStyles.Top | AnchorStyles.Right)));
            buttonTime.BackgroundImage = global::NoteBox.Properties.Resources.ButtonTime;
            buttonTime.FlatStyle = FlatStyle.Popup;
            buttonTime.Font = new System.Drawing.Font("MS Outlook", 5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonTime.Location = new System.Drawing.Point(236, 3);
            buttonTime.Margin = new Padding(0);
            buttonTime.Name = "buttonTime";
            buttonTime.Size = new System.Drawing.Size(16, 15);
            buttonTime.TabIndex = 2;
            buttonTime.UseVisualStyleBackColor = true;
            toolTipa.SetToolTip(buttonTime, "Установить время выполнения");
            buttonTime.Click+=new EventHandler(mbuttonTime_Click);
            // 
            // richTextBox1
            // 
            richTextBox1.Anchor = ((AnchorStyles)(((AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom)
                        | AnchorStyles.Right)));
            richTextBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(8)))), ((int)(((byte)(28)))), ((int)(((byte)(61)))));
            richTextBox1.BorderStyle = BorderStyle.None;
            richTextBox1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            richTextBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(153)))), ((int)(((byte)(180)))), ((int)(((byte)(210)))));
            richTextBox1.Location = new System.Drawing.Point(27, 20);
            richTextBox1.Name = "richTextBox1";
            richTextBox1.Size = new System.Drawing.Size(224, 75);
            richTextBox1.TabIndex = 1;
            richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
            // 
            // buttonTaskDown
            // 
            buttonTaskDown.BackgroundImage = global::NoteBox.Properties.Resources.TaskDown;
            buttonTaskDown.FlatStyle = FlatStyle.Popup;
            buttonTaskDown.Font = new System.Drawing.Font("MS Outlook", 5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonTaskDown.Location = new System.Drawing.Point(6, 71);
            buttonTaskDown.Margin = new Padding(0);
            buttonTaskDown.Name = "buttonTaskDown";
            buttonTaskDown.Size = new System.Drawing.Size(16, 10);
            buttonTaskDown.TabIndex = 10;
            buttonTaskDown.UseVisualStyleBackColor = true;
            buttonTaskDown.Click+=new EventHandler(mbuttonTaskDown_Click);
            toolTipa.SetToolTip(buttonTaskDown, "Переместить задание вниз");
            // 
            // buttonTaskUP
            // 
            buttonTaskUP.BackgroundImage = global::NoteBox.Properties.Resources.TaskUp;
            buttonTaskUP.FlatStyle = FlatStyle.Popup;
            buttonTaskUP.Font = new System.Drawing.Font("MS Outlook", 5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonTaskUP.Location = new System.Drawing.Point(6, 61);
            buttonTaskUP.Margin = new Padding(0);
            buttonTaskUP.Name = "buttonTaskUP";
            buttonTaskUP.Size = new System.Drawing.Size(16, 10);
            buttonTaskUP.TabIndex = 9;
            buttonTaskUP.UseVisualStyleBackColor = true;
            buttonTaskUP.Click += new EventHandler(mbuttonTaskUP_Click);
            toolTipa.SetToolTip(buttonTaskUP, "Переместить задание вверх");
            // 
            // buttonTaskClose
            // 
            buttonTaskClose.BackgroundImage = global::NoteBox.Properties.Resources.ButtonDel;
            buttonTaskClose.FlatStyle = FlatStyle.Popup;
            buttonTaskClose.Font = new System.Drawing.Font("MS Outlook", 5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            buttonTaskClose.Location = new System.Drawing.Point(6, 32);
            buttonTaskClose.Margin = new Padding(0);
            buttonTaskClose.Name = "buttonTaskClose";
            buttonTaskClose.Size = new System.Drawing.Size(16, 15);
            buttonTaskClose.TabIndex = 7;
            buttonTaskClose.UseVisualStyleBackColor = true;
            buttonTaskClose.Click+=new EventHandler(mbuttonTaskClose_Click);
            toolTipa.SetToolTip(buttonTaskClose, "Удалить задание");
            // 
            // buttonStrikeout
            // 
            this.buttonStrikeout.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(153)))), ((int)(((byte)(180)))), ((int)(((byte)(210)))));
            this.buttonStrikeout.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.buttonStrikeout.Location = new System.Drawing.Point(145, 11);
            this.buttonStrikeout.Name = "buttonStrikeout";
            this.buttonStrikeout.Size = new System.Drawing.Size(85, 1);
            this.buttonStrikeout.TabIndex = 22;
            this.buttonStrikeout.UseVisualStyleBackColor = false;
            this.buttonStrikeout.Visible = false;
            // 
            // zbuttonpanel
            // 
            this.zbuttonpanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.zbuttonpanel.BackColor = System.Drawing.Color.Black;
            this.zbuttonpanel.Enabled = false;
            this.zbuttonpanel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.zbuttonpanel.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(55)))), ((int)(((byte)(84)))), ((int)(((byte)(116)))));
            this.zbuttonpanel.Location = new System.Drawing.Point(0, 0);
            this.zbuttonpanel.Name = "zbuttonpanel";
            this.zbuttonpanel.Size = new System.Drawing.Size(257, 100);
            this.zbuttonpanel.TabIndex = 26;
            this.zbuttonpanel.UseVisualStyleBackColor = false;
            // 
            // zbuttonrichtext
            // 
            this.zbuttonrichtext.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.zbuttonrichtext.BackColor = System.Drawing.Color.Black;
            this.zbuttonrichtext.Enabled = false;
            this.zbuttonrichtext.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.zbuttonrichtext.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(55)))), ((int)(((byte)(84)))), ((int)(((byte)(116)))));
            this.zbuttonrichtext.Location = new System.Drawing.Point(26, 19);
            this.zbuttonrichtext.Name = "zbuttonrichtext";
            this.zbuttonrichtext.Size = new System.Drawing.Size(226, 77);
            this.zbuttonrichtext.TabIndex = 27;
            this.zbuttonrichtext.UseVisualStyleBackColor = false;

            textBox1.Focus();
        }

        void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            Form1.TaskChanged = true;
        }

        void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (textBox1.Text.Length > 20)
            {
                if (e.KeyChar == 8)
                    return;
                e.KeyChar = Convert.ToChar(0);
            }
            Form1.TaskChanged = true;
        }

             
        void mbuttonTaskHide_Click(object sender, EventArgs e)
        {
            HidePressed(id);
        }
        void mbuttonTaskClose_Click(object sender, EventArgs e)
        {
            if ((richTextBox1.Text.Length == 0) || (richTextBox1.Text == "Введите задание"))
                if ((textBox1.Text.Length == 0) || (textBox1.Text == "Заголовок"))
                {
                    ClosePressed(id);
                    return;
                }
            panelDel.Visible = true;
        }
        void mbuttonDelYes_Click(object sender, EventArgs e)
        {
            panelDel.Visible = false;
            ClosePressed(id);
        }
        void mbuttonDelNo_Click(object sender, EventArgs e)
        {
            panelDel.Visible = false;
        }
        void mbuttonTaskUP_Click(object sender, EventArgs e)
        {
            TaskUpPressed(id);
        }
        void mbuttonTaskDown_Click(object sender, EventArgs e)
        {
            TaskDownPressed(id);
        }
        void mbuttonTime_Click(object sender, EventArgs e)
        {
            TimePressed(id);
        }

        public void SetActive(bool val)
        {
            if (!val)
            {
                active = false;
                buttonStrikeout.Visible = true;
                toolTipa.SetToolTip(labelTime, "Напоминание выполнено");
                buttonStrikeout.BringToFront();
            }
            else
            {
                active = true;
                buttonStrikeout.Visible = false;
            }
        }
        //
        bool formMoved = false;
        int YPos = 0;
        //
        public int lastFormHeight = 0;
        public int lastHeight = 0;
        //
        private void Resize_MouseDown(object sender, MouseEventArgs e)
        {
            if (sender is Panel && Height != Form1.hideTaskHeight)
            {
                if (e.Y < ((Panel)sender).Height - 7)
                    return;
                YPos = e.Y;
                lastFormHeight = Parent.Height - Height;
                lastHeight = Height;
                formMoved = true;
            }
        }
        private void Resize_MouseMove(object sender, MouseEventArgs e)
        {
            if (sender is Panel && Height != Form1.hideTaskHeight)
            {
                if (e.Y > ((Panel)sender).Height - 7)
                    Cursor.Current = Cursors.HSplit;
                else
                    Cursor.Current = Cursors.Default;
            }
            if (formMoved)
            {
                if (sender is Panel)
                {
                    if (e.Y < 85)
                        return;
                    ((Panel)sender).Height = e.Y;
                    PanelOldHeight = Height;
                    HeightResized(id);

                }
            }
        }
        private void Resize_MouseUp(object sender, MouseEventArgs e)
        {
            if (formMoved)
            {
                formMoved = false;
                PanelOldHeight = Height;
            }            
        }

 
    }
}
